package com.redpacketcover.rc.base;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 懒加载   主要是让数据在该Fragment可见的时候加载
 * initViews（）初始化View
 * initData();加载网络数据  该方法在fragment可见的时候调用
 */

public abstract class BasicLazyFragment<P extends BasicIPresenter> extends Fragment implements BasicView {

    private boolean isFragmentVisible;//是否可见状态
    private boolean isPrepared;//标志位，View已经初始化完成。
    private boolean isFirstLoad = true;//是否第一次加载

    public P mPresenter;
    public Activity mActivity;
    private View mContentView = null;
    protected Unbinder mUnBinder;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(initLayout(), container, false);
        mUnBinder = ButterKnife.bind(this, mContentView);

        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        initViews(mContentView);
        isPrepared = true;
        isFirstLoad = true;
        lazyLoad();
        return mContentView;
    }

    /**
     * 要实现延迟加载Fragment内容
     */
    protected void lazyLoad() {
        if (isPrepared && isFragmentVisible) {
            if (isFirstLoad) {
                isFirstLoad = false;
                initData();
            }
        }
    }

    /**
     * 如果是与ViewPager一起使用，调用的是setUserVisibleHint
     *
     * @param isVisibleToUser 是否显示出来了
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isFragmentVisible = true;
            lazyLoad();
        } else {
            isFragmentVisible = false;
        }
    }

    /**
     * 如果是通过FragmentTransaction的show和hide的方法来控制显示，调用的是onHiddenChanged.
     * 若是初始就show的Fragment 为了触发该事件 需要先hide再show
     *
     * @param hidden hidden True if the fragment is now hidden, false if it is not
     *               visible.
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            isFragmentVisible = true;
            lazyLoad();
        } else {
            isFragmentVisible = false;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (mActivity != null) {
            mActivity = null;
        }
        if (mUnBinder != null) {
            mUnBinder.unbind();//解绑
        }
    }


    protected abstract P createPresenter(); //MVP模式重写这个方法，

    protected abstract void initViews(View view);  // 初始化view

    protected abstract void initData();

    protected abstract int initLayout();//设置布局


}
